//
//  ZDDComposeToolBar.swift
//  小蜗牛_新浪微博
//
//  Created by 周冬冬 on 16/7/3.
//  Copyright © 2016年 我的mac. All rights reserved.
//

import UIKit
// toolbar 按钮枚举
enum ZDDComposeToolbarType: Int{
    // 图片
    case Picture = 10
    // @
    case Mention = 20
    // #
    case Trend = 30
    // 表情
    case Emoticon = 40
    // +
    case Add = 50
}
class ZDDComposeToolBar: UIView {
    
    var emoticonButton:UIButton?
    var isEmoticon:Bool = false{
        didSet{
        
            //显示加盘
            if isEmoticon {
            
                emoticonButton?.setImage(UIImage(named: "compose_keyboardbutton_background"), forState: UIControlState.Normal)
                emoticonButton?.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), forState: UIControlState.Highlighted)
            
            }else{
                // 显示笑脸
                emoticonButton?.setImage(UIImage(named: "compose_emoticonbutton_background"), forState: UIControlState.Normal)
                emoticonButton?.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), forState: UIControlState.Highlighted)
            }
        
        }
    
    
    }
    // 告知外界按钮点击啦
    var closure:((type: ZDDComposeToolbarType)->())?
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func addChildButtons(imageName:String,type:ZDDComposeToolbarType)->UIButton{
        let button = UIButton()
        button.tag = type.rawValue
        // 添加点击事件
        button.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        button.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        button.setImage(UIImage(named:"\(imageName)_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Highlighted)
        addSubview(button)
    return button
    }
    //点击
    func buttonClick(button:UIButton){
    
        closure?(type: ZDDComposeToolbarType(rawValue: button.tag)!)
    
    }
    private func setupUI(){
    
        backgroundColor = RandomColor()
    
        //添加控件
        let pictureButton = addChildButtons("compose_toolbar_picture", type: .Picture)
        let mentionButton = addChildButtons("compose_mentionbutton_background", type: .Mention)
        let trendButton = addChildButtons("compose_trendbutton_background", type: .Trend)
            emoticonButton = addChildButtons("compose_emoticonbutton_background", type: .Emoticon)
        let addButton = addChildButtons("compose_add_background", type: .Add)
        //添加约束
        pictureButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.leading.equalTo(self)
            make.width.equalTo(mentionButton)
        }
        mentionButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(pictureButton.snp_trailing)
            make.width.equalTo(trendButton)
        }
        trendButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(mentionButton.snp_trailing)
            make.width.equalTo(emoticonButton!)
        }
        emoticonButton!.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(trendButton.snp_trailing)
            make.width.equalTo(addButton)
        }
        addButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.trailing.equalTo(self)
            
            make.leading.equalTo(emoticonButton!.snp_trailing)
        }
        
    }
    
}
